package bind;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.taobao.ibatiside.model.configv2.Configv2Factory;
import org.taobao.ibatiside.model.configv2.SqlMapType;
import org.eclipse.emf.databinding.EMFObservables;
import org.taobao.ibatiside.model.configv2.Configv2Package.Literals;

public class Main {
	private DataBindingContext m_bindingContext;

	protected Shell shell;
	private Text text;
	private Text text_1;
	private HelloBean bean;
	private SqlMapType type2;
	private Text text_2;
	private Label lblNewLabel;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
			public void run() {
				try {
					Main window = new Main();
					window.open();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		bean = new HelloBean();
		type2 = Configv2Factory.eINSTANCE.createSqlMapType();
		shell = new Shell();
		shell.setSize(450, 300);
		shell.setText("SWT Application");
		
		Label lblHello = new Label(shell, SWT.NONE);
		lblHello.setBounds(10, 10, 61, 17);
		lblHello.setText("Hello");
		
		text = new Text(shell, SWT.BORDER);
		text.setBounds(78, 7, 73, 23);
		
		text_1 = new Text(shell, SWT.BORDER);
		text_1.setBounds(78, 36, 73, 23);
		
		text_2 = new Text(shell, SWT.BORDER);
		text_2.setBounds(27, 77, 213, 23);
		
		lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setBounds(29, 112, 61, 17);
		m_bindingContext = initDataBindings();

	}
	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		IObservableValue textObserveTextObserveWidget = SWTObservables.observeText(text, SWT.Modify);
		IObservableValue beanHelloObserveValue = BeansObservables.observeValue(bean, "hello");
		bindingContext.bindValue(textObserveTextObserveWidget, beanHelloObserveValue, null, null);
		//
		IObservableValue text_1ObserveTextObserveWidget = SWTObservables.observeText(text_1, SWT.Modify);
		bindingContext.bindValue(text_1ObserveTextObserveWidget, beanHelloObserveValue, null, null);
		//
		IObservableValue lblNewLabelObserveTextObserveWidget = SWTObservables.observeText(lblNewLabel);
		IObservableValue type2ResourceObserveValue = EMFObservables.observeValue(type2, Literals.SQL_MAP_TYPE__RESOURCE);
		bindingContext.bindValue(lblNewLabelObserveTextObserveWidget, type2ResourceObserveValue, null, null);
		//
		IObservableValue text_2ObserveTextObserveWidget = SWTObservables.observeText(text_2, SWT.Modify);
		bindingContext.bindValue(text_2ObserveTextObserveWidget, type2ResourceObserveValue, null, null);
		//
		return bindingContext;
	}
}
